package com.ck.express.db.dao

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.Query
import androidx.room.Update
import com.ck.express.db.entity.FailedDeliveryRequest
import kotlinx.coroutines.flow.Flow

@Dao
interface FailedDeliveryRequestDao {
    @Insert
    suspend fun insert(request: FailedDeliveryRequest): Long

    @Update
    suspend fun update(request: FailedDeliveryRequest)

    @Query("SELECT * FROM failed_delivery_requests WHERE waybillNo = :waybillNo")
    suspend fun getByWaybillNo(waybillNo: String): FailedDeliveryRequest?

    @Query("SELECT * FROM failed_delivery_requests ORDER BY createTimeStamp DESC")
    fun getAllFailedRequests(): Flow<List<FailedDeliveryRequest>>

    @Query("DELETE FROM failed_delivery_requests WHERE id = :id")
    suspend fun deleteById(id: Long)

    @Query("DELETE FROM failed_delivery_requests")
    suspend fun deleteAll()

    @Query("SELECT * FROM failed_delivery_requests ORDER BY createTimeStamp DESC LIMIT :limit OFFSET :offset")
    suspend fun getFailedRequestsByPage(offset: Int, limit: Int): List<FailedDeliveryRequest>

    @Query("SELECT COUNT(*) FROM failed_delivery_requests")
    suspend fun getTotalCount(): Int


}